package cn.com.nes.site.entity.iot;

import cn.com.nes.mybatis.agent.ope.entity.AStandardDataItem;
import cn.com.nes.mybatis.agent.ope.entity.EquipmentControlParam;
import cn.com.nes.mybatis.agent.ope.entity.IsEquipmentFunctionDetailInfo;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import java.util.*;

public class ControlCommandForEquipmentControlParam {
    private String manufactureId;
    private String protocolId;
    private String protocolName;
    private String fieldName;
    private List<IsEquipmentFunctionDetailInfo> detailInfoList;

    private String functionName;

    private String controlCommandId;

    private String dataItemId;

    private String classify;

    public String getClassify() {
        return classify;
    }

    public void setClassify(String classify) {
        this.classify = classify;
    }

    public List<EquipmentControlParam> toEquipmentControlParam()
    {

        List<EquipmentControlParam> result = new ArrayList<>();
        if (CollUtil.isNotEmpty(detailInfoList)) {

            Set<String> codes = CollUtil.newHashSet();
            for (IsEquipmentFunctionDetailInfo detailInfo : detailInfoList) {
                codes.add(detailInfo.getCode());
            }

            for (String code : codes) {
                List<IsEquipmentFunctionDetailInfo> splitDetailInfoList = CollUtil.newArrayList();
                for (IsEquipmentFunctionDetailInfo detailInfo : detailInfoList) {
                    if (code.equals(detailInfo.getCode())) {
                        splitDetailInfoList.add(detailInfo);
                    }
                }
                EquipmentControlParam equipmentControlParam = new EquipmentControlParam();
                equipmentControlParam.setManufactureId(this.manufactureId);
                equipmentControlParam.setProtocolId(this.protocolId);
                equipmentControlParam.setProtocolName(this.protocolName);
                equipmentControlParam.setFieldName(this.fieldName);
                equipmentControlParam.setFunctionName(this.functionName);
                equipmentControlParam.setGroupName(this.classify);
                equipmentControlParam.setGroupCode(this.classify);
                equipmentControlParam.setControlCommandId(this.controlCommandId);

                equipmentControlParam.setSort(1);
                result.add(equipmentControlParam);
                IsEquipmentFunctionDetailInfo detail = splitDetailInfoList.get(0);
//                this.code = code;
                JSONArray jsonArray = new JSONArray();
                if (splitDetailInfoList.size() == 1) {
                    //
                    if(ObjectUtil.isNotNull(detail.getValue())){
                        equipmentControlParam.setFunctionCode(detail.getFunctionCode());
                        equipmentControlParam.setMaxValue(detail.getUpLimit());
                        equipmentControlParam.setMinValue(detail.getLowLimit());
                        equipmentControlParam.setFunctionType("4");
                    }else{
                        equipmentControlParam.setFunctionCode(detail.getFunctionCode());
                        equipmentControlParam.setMaxValue(detail.getUpLimit());
                        equipmentControlParam.setMinValue(detail.getLowLimit());
                        equipmentControlParam.setFunctionType("3");
                    }

                } else {
                    // code相同的有多个
                    equipmentControlParam.setFunctionCode(detail.getCode());
                    //if (splitDetailInfoList.size() == 2) {
                    //                    if (splitDetailInfoList.size() > 2) {
                    // modify by zhouy
                    // 1布尔型，2枚举型，3无值控制，4数值控制
                    if (splitDetailInfoList.size() == 2) {
                        equipmentControlParam.setFunctionType("2");
                    } else {
                        equipmentControlParam.setFunctionType("1");
                    }
                }

                for (IsEquipmentFunctionDetailInfo detailInfo : splitDetailInfoList)
                {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("code", detailInfo.getFunctionCode());
                    jsonObject.put("original_value", detailInfo.getValue());
                    jsonObject.put("name", detailInfo.getName());
                    jsonArray.add(jsonObject);
                }
                equipmentControlParam.setListVal(jsonArray.toJSONString());
            }
        }
        return result;
    }

    public String getManufactureId() {
        return manufactureId;
    }

    public void setManufactureId(String manufactureId) {
        this.manufactureId = manufactureId;
    }

    public String getProtocolId() {
        return protocolId;
    }

    public void setProtocolId(String protocolId) {
        this.protocolId = protocolId;
    }

    public String getProtocolName() {
        return protocolName;
    }

    public void setProtocolName(String protocolName) {
        this.protocolName = protocolName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public void setFieldName(String fieldName) {
        this.fieldName = fieldName;
    }

    public List<IsEquipmentFunctionDetailInfo> getDetailInfoList() {
        return detailInfoList;
    }

    public void setDetailInfoList(List<IsEquipmentFunctionDetailInfo> detailInfoList) {
        this.detailInfoList = detailInfoList;
    }

    public String getControlCommandId() {
        return controlCommandId;
    }

    public void setControlCommandId(String controlCommandId) {
        this.controlCommandId = controlCommandId;
    }

    public String getFunctionName() {
        return functionName;
    }

    public void setFunctionName(String functionName) {
        this.functionName = functionName;
    }

    public EquipmentControlParam addSetTemp(List<ControlCommandForEquipmentControlParam> setTempParams,
                                            List<AStandardDataItem> setTempStandardDataItem) {
        Map<String, ControlCommandForEquipmentControlParam> setTempMap = new HashMap<>();
        if (CollUtil.isNotEmpty(setTempStandardDataItem)) {
            for (ControlCommandForEquipmentControlParam param : setTempParams) {
                setTempMap.put(param.getDetailInfoList().get(0).getDetailId(), param);
            }
        }
        Map<Integer, JSONObject> map = new HashMap<>();

        if (CollUtil.isNotEmpty(setTempStandardDataItem)) {
            for (AStandardDataItem t : setTempStandardDataItem) {
                ControlCommandForEquipmentControlParam param = setTempMap.get(t.getFunctionDetailId());
                if(param!=null) {
                    final IsEquipmentFunctionDetailInfo detail = param.getDetailInfoList().get(0);
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("maxValue", detail.getUpLimit());
                    jsonObject.put("minValue", detail.getLowLimit());
                    jsonObject.put("temperatureCode", detail.getFunctionCode());
                    jsonObject.put("field", param.getFieldName());
                    map.put(t.getDataItemValue(), jsonObject);
                }
            }
        }

        EquipmentControlParam equipmentControlParam = new EquipmentControlParam();
        equipmentControlParam.setManufactureId(this.manufactureId);
        equipmentControlParam.setProtocolId(this.protocolId);
        equipmentControlParam.setProtocolName(this.protocolName);
        equipmentControlParam.setFieldName(this.fieldName);
        equipmentControlParam.setFunctionName(this.functionName);
        equipmentControlParam.setSort(1);
        equipmentControlParam.setGroupName(this.classify);
        equipmentControlParam.setGroupCode(this.classify);
        equipmentControlParam.setControlCommandId(this.getControlCommandId());

        JSONArray jsonArray = new JSONArray();
        IsEquipmentFunctionDetailInfo detail = detailInfoList.get(0);
//        this.code = detail.getCode();
        equipmentControlParam.setFunctionCode(detail.getCode());
        equipmentControlParam.setFunctionType("2");
        for (IsEquipmentFunctionDetailInfo detailInfo : detailInfoList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", detailInfo.getFunctionCode());
            jsonObject.put("original_value", detailInfo.getValue());
            jsonObject.put("name", detailInfo.getName());
            // 获取模式对应的设定温度
            JSONObject t = map.get(detailInfo.getValue().intValue());
            // add by zhouy
            if(t!=null) {
                jsonObject.putAll(t);
                jsonArray.add(jsonObject);
            }
        }
        equipmentControlParam.setListVal(jsonArray.toJSONString());
        return equipmentControlParam;
    }

    public String getDataItemId() {
        return dataItemId;
    }

    public void setDataItemId(String dataItemId) {
        this.dataItemId = dataItemId;
    }
}
